<?php
namespace haibao\app\data;

class ArticleProperty extends \haibao\app\data\BaseMysql{

	public function __construct(){
		parent::__construct('\haibao\app\model\data\ArticleProperty');
	}

	/**
	 * 修改文章点赞数量
	 */
	public function unsetPraiseCount($articleId){
	    $this->query('update ArticleProperty set PraiseNum=(select count(*) from ArticlePraise where ArticleId=%s)where ArticleId=%s', array(
	        $articleId, $articleId 
	    ));
	}
	
	/**
	 * 统计文章属性
	 */
	public function getArticleProperty($startData, $endData){
		//获得文章总数,文章总点击量,总评论数,总投票数,app点击量,wap点击量,总点赞数
		$resultAllClick = $this->getAllCount($startData, $endData);

		$data = array(
				'Num' => $resultAllClick['Num'],
				'ClickNum' => $resultAllClick['ClickNum'],
				'CommentNum' => $resultAllClick['CommentNum'],
		        'PraiseNum' => $resultAllClick['PraiseNum'],
				'VoteNum' => $resultAllClick['VoteNum'],
				'AppClickNum' => $resultAllClick['AppClickNum'],
				'WapClickNum' => $resultAllClick['WapClickNum'],
				'ShareNum' => $resultAllClick['ShareNum'],
		);
		return $data;
	}
	
	/**
	 * 根据文章id查询文章评论数和点赞数
	 */
	public function getArticlePropertyByArticleId($ids){
	    $str = preg_replace('/(\d+)/', '%s', implode(',', $ids));
	    $result = $this->query("select ArticleId,ClickNum,PraiseNum from ArticleProperty where ArticleId in ($str)", $ids);
	    $retArr = array();
	    while($row = $result->fetch_assoc()){
	        $tmp = array();
	        $tmp['ArticleId'] = intval($row['ArticleId']);
	        $tmp['ClickNum'] = intval($row['ClickNum']);
	        $tmp['PraiseNum'] = intval($row['PraiseNum']);
	        array_push($retArr, $tmp);
	    }
	    return $retArr;
	}
	
	/**
	 * 获得所有文章总点击量,总评论数,总投票数,app点击数,wap点击数
	 */
	private function getAllCount($startData, $endData){
		$sqlAllClick = "select count(*) as Num,
			sum(ClickNum) AS ClickNum,
			sum(CommentNum) AS CommentNum,
			sum(VoteNum) AS VoteNum,
			sum(AppClickNum) AS AppClickNum,
			sum(WapClickNum) AS WapClickNum,
		    sum(PraiseNum) AS PraiseNum,
			sum(ShareNum) AS ShareNum from `ArticleProperty` as a left join `Article` as b on a.ArticleId = b.Id where b.PublishTime >= %s and b.PublishTime <= %s and b.Type = 1";
		return $this->query($sqlAllClick,array($startData,$endData))->fetch_assoc();
	} 

}